// <copyright file="ProductService.cs" company="EnterpriseApi">
// Copyright (c) EnterpriseApi. All rights reserved.
// </copyright>

using EnterpriseApi.Models;
using EnterpriseApi.Repositories;

namespace EnterpriseApi.Services;

/// <summary>
/// 产品服务实现类，提供产品特有的业务逻辑操作
/// </summary>
public class ProductService : Service<Product>, IProductService
{
    private readonly IProductRepository _productRepository;

    /// <summary>
    /// 初始化ProductService的新实例
    /// </summary>
    /// <param name="productRepository">产品仓储</param>
    public ProductService(IProductRepository productRepository) : base(productRepository)
    {
        _productRepository = productRepository;
    }

    /// <summary>
    /// 根据类别获取产品列表
    /// </summary>
    /// <param name="categoryId">类别ID</param>
    /// <returns>特定类别的产品集合</returns>
    public async Task<IEnumerable<Product>> GetProductsByCategoryAsync(int categoryId)
    {
        return await _productRepository.GetProductsByCategoryAsync(categoryId);
    }

    /// <summary>
    /// 获取可用产品列表
    /// </summary>
    /// <returns>可用产品集合</returns>
    public async Task<IEnumerable<Product>> GetAvailableProductsAsync()
    {
        return await _productRepository.GetAvailableProductsAsync();
    }

    /// <summary>
    /// 根据价格范围获取产品
    /// </summary>
    /// <param name="minPrice">最低价格</param>
    /// <param name="maxPrice">最高价格</param>
    /// <returns>价格范围内的产品集合</returns>
    public async Task<IEnumerable<Product>> GetProductsByPriceRangeAsync(decimal minPrice, decimal maxPrice)
    {
        if (minPrice < 0 || maxPrice < 0 || minPrice > maxPrice)
        {
            throw new ArgumentException("价格范围无效");
        }

        return await _productRepository.GetProductsByPriceRangeAsync(minPrice, maxPrice);
    }

    /// <summary>
    /// 搜索产品（按名称或描述）
    /// </summary>
    /// <param name="searchTerm">搜索关键词</param>
    /// <returns>符合搜索条件的产品集合</returns>
    public async Task<IEnumerable<Product>> SearchProductsAsync(string searchTerm)
    {
        return await _productRepository.SearchProductsAsync(searchTerm);
    }

    /// <summary>
    /// 更新产品库存
    /// </summary>
    /// <param name="productId">产品ID</param>
    /// <param name="quantity">要增加或减少的数量（负数表示减少）</param>
    /// <returns>更新是否成功</returns>
    public async Task<bool> UpdateProductStockAsync(int productId, int quantity)
    {
        var product = await _productRepository.GetByIdAsync(productId);
        if (product == null || !product.IsAvailable)
        {
            return false;
        }

        // 检查库存是否足够（如果是减少库存）
        if (quantity < 0 && product.StockQuantity < Math.Abs(quantity))
        {
            return false;
        }

        product.StockQuantity += quantity;
        product.UpdatedAt = DateTime.UtcNow;

        await _productRepository.UpdateAsync(product);
        return true;
    }

    /// <summary>
    /// 检查产品库存是否足够
    /// </summary>
    /// <param name="productId">产品ID</param>
    /// <param name="quantity">所需数量</param>
    /// <returns>库存是否足够</returns>
    public async Task<bool> CheckStockAvailabilityAsync(int productId, int quantity)
    {
        if (quantity <= 0)
        {
            throw new ArgumentException("数量必须大于0", nameof(quantity));
        }

        return await _productRepository.CheckStockAvailabilityAsync(productId, quantity);
    }
}